package com.zenithmind.news.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.news.pojo.entity.News;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 新闻Mapper接口
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Mapper
public interface NewsMapper extends BaseMapper<News> {

    /**
     * 分页查询新闻列表
     * 
     * @param page 分页参数
     * @param categoryId 分类ID
     * @param status 新闻状态
     * @param type 新闻类型
     * @param keyword 关键词
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 新闻列表
     */
    IPage<News> selectNewsPage(Page<News> page,
                              @Param("categoryId") String categoryId,
                              @Param("status") Integer status,
                              @Param("type") Integer type,
                              @Param("keyword") String keyword,
                              @Param("startTime") LocalDateTime startTime,
                              @Param("endTime") LocalDateTime endTime);

    /**
     * 查询热门新闻
     * 
     * @param limit 限制数量
     * @return 新闻列表
     */
    List<News> selectHotNews(@Param("limit") Integer limit);

    /**
     * 查询推荐新闻
     * 
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 新闻列表
     */
    List<News> selectRecommendNews(@Param("userId") String userId,
                                  @Param("limit") Integer limit);

    /**
     * 查询置顶新闻
     * 
     * @param categoryId 分类ID
     * @param limit 限制数量
     * @return 新闻列表
     */
    List<News> selectTopNews(@Param("categoryId") String categoryId,
                            @Param("limit") Integer limit);

    /**
     * 查询相关新闻
     * 
     * @param newsId 新闻ID
     * @param limit 限制数量
     * @return 新闻列表
     */
    List<News> selectRelatedNews(@Param("newsId") String newsId,
                                @Param("limit") Integer limit);

    /**
     * 根据标签查询新闻
     * 
     * @param tags 标签列表
     * @param limit 限制数量
     * @return 新闻列表
     */
    List<News> selectNewsByTags(@Param("tags") List<String> tags,
                               @Param("limit") Integer limit);

    /**
     * 根据关键词搜索新闻
     * 
     * @param keyword 关键词
     * @param limit 限制数量
     * @return 新闻列表
     */
    List<News> searchNewsByKeyword(@Param("keyword") String keyword,
                                  @Param("limit") Integer limit);

    /**
     * 更新新闻统计信息
     * 
     * @param newsId 新闻ID
     * @param viewCount 阅读量增量
     * @param likeCount 点赞数增量
     * @param favoriteCount 收藏数增量
     * @param shareCount 分享数增量
     * @param commentCount 评论数增量
     * @return 更新行数
     */
    int updateStatistics(@Param("newsId") String newsId,
                        @Param("viewCount") Long viewCount,
                        @Param("likeCount") Long likeCount,
                        @Param("favoriteCount") Long favoriteCount,
                        @Param("shareCount") Long shareCount,
                        @Param("commentCount") Long commentCount);

    /**
     * 更新新闻热度评分
     * 
     * @param newsId 新闻ID
     * @param hotScore 热度评分
     * @return 更新行数
     */
    int updateHotScore(@Param("newsId") String newsId,
                      @Param("hotScore") Double hotScore);

    /**
     * 批量更新新闻状态
     * 
     * @param newsIds 新闻ID列表
     * @param status 新闻状态
     * @return 更新行数
     */
    int batchUpdateStatus(@Param("newsIds") List<String> newsIds,
                         @Param("status") Integer status);

    /**
     * 查询新闻统计信息
     * 
     * @param categoryId 分类ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 统计信息
     */
    NewsStatistics selectNewsStatistics(@Param("categoryId") String categoryId,
                                       @Param("startTime") LocalDateTime startTime,
                                       @Param("endTime") LocalDateTime endTime);

    /**
     * 查询热门标签
     * 
     * @param limit 限制数量
     * @return 标签列表
     */
    List<TagStatistics> selectHotTags(@Param("limit") Integer limit);

    /**
     * 查询新闻趋势
     * 
     * @param days 天数
     * @return 趋势数据
     */
    List<NewsTrend> selectNewsTrend(@Param("days") Integer days);

    /**
     * 新闻统计信息
     */
    class NewsStatistics {
        private Long totalNews;
        private Long publishedNews;
        private Long draftNews;
        private Long totalViews;
        private Long totalLikes;
        private Long totalComments;
        private Long totalShares;
        private String topCategory;
        private String topAuthor;

        // Getters and Setters
        public Long getTotalNews() { return totalNews; }
        public void setTotalNews(Long totalNews) { this.totalNews = totalNews; }
        
        public Long getPublishedNews() { return publishedNews; }
        public void setPublishedNews(Long publishedNews) { this.publishedNews = publishedNews; }
        
        public Long getDraftNews() { return draftNews; }
        public void setDraftNews(Long draftNews) { this.draftNews = draftNews; }
        
        public Long getTotalViews() { return totalViews; }
        public void setTotalViews(Long totalViews) { this.totalViews = totalViews; }
        
        public Long getTotalLikes() { return totalLikes; }
        public void setTotalLikes(Long totalLikes) { this.totalLikes = totalLikes; }
        
        public Long getTotalComments() { return totalComments; }
        public void setTotalComments(Long totalComments) { this.totalComments = totalComments; }
        
        public Long getTotalShares() { return totalShares; }
        public void setTotalShares(Long totalShares) { this.totalShares = totalShares; }
        
        public String getTopCategory() { return topCategory; }
        public void setTopCategory(String topCategory) { this.topCategory = topCategory; }
        
        public String getTopAuthor() { return topAuthor; }
        public void setTopAuthor(String topAuthor) { this.topAuthor = topAuthor; }
    }

    /**
     * 标签统计信息
     */
    class TagStatistics {
        private String tag;
        private Long count;
        private Double score;

        // Getters and Setters
        public String getTag() { return tag; }
        public void setTag(String tag) { this.tag = tag; }
        
        public Long getCount() { return count; }
        public void setCount(Long count) { this.count = count; }
        
        public Double getScore() { return score; }
        public void setScore(Double score) { this.score = score; }
    }

    /**
     * 新闻趋势
     */
    class NewsTrend {
        private String date;
        private Long newsCount;
        private Long viewCount;
        private Long likeCount;
        private Long commentCount;

        // Getters and Setters
        public String getDate() { return date; }
        public void setDate(String date) { this.date = date; }
        
        public Long getNewsCount() { return newsCount; }
        public void setNewsCount(Long newsCount) { this.newsCount = newsCount; }
        
        public Long getViewCount() { return viewCount; }
        public void setViewCount(Long viewCount) { this.viewCount = viewCount; }
        
        public Long getLikeCount() { return likeCount; }
        public void setLikeCount(Long likeCount) { this.likeCount = likeCount; }
        
        public Long getCommentCount() { return commentCount; }
        public void setCommentCount(Long commentCount) { this.commentCount = commentCount; }
    }
}
